package com.snake.springboot.authority.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.snake.springboot.authority.api.model.AuthSysAdminModel;
import com.snake.springboot.authority.api.request.AuthSysAdminChangeReq;
import com.snake.springboot.authority.api.request.AuthSysAdminQuery;
import com.snake.springboot.authority.api.service.AuthSysAdminService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthSysAdminDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthSysAdminMapper;
import com.snake.springboot.authority.service.assembler.AuthSysAdminAssembler;
import org.snake.common.result.ReturnList;
import org.snake.common.security.MD5Util;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类 {@code AuthSysAdminServiceImpl}  <br> .
 * <p>权限管理-系统管理员实现类
 * <p>创建时间：2025/01/13
 *
 * @author Huang.yj
 */
@Service
public class AuthSysAdminServiceImpl implements AuthSysAdminService {

    private AuthSysAdminMapper authSysAdminMapper;

    @Autowired
    public AuthSysAdminServiceImpl(AuthSysAdminMapper authSysAdminMapper){
        this.authSysAdminMapper = authSysAdminMapper;
    }

    @Override
    public AuthSysAdminModel addSystemAdmin(AuthSysAdminModel model) throws Exception {
        AuthSysAdminDO sysAdminDO = AuthSysAdminAssembler.assembleSysAdminDO(model,true);
        sameAccountVerify(sysAdminDO);
        authSysAdminMapper.insert(sysAdminDO);
        model.setSysAdminId(sysAdminDO.getSysAdminId());
        return model;
    }

    @Override
    public AuthSysAdminModel modifySystemAdmin(AuthSysAdminModel model) throws Exception {
        AuthSysAdminDO sysAdminDO = AuthSysAdminAssembler.assembleSysAdminDO(model,false);
        sameAccountVerify(sysAdminDO);
        authSysAdminMapper.update(sysAdminDO);
        return model;
    }

    @Override
    public int removeSystemAdmin(String sysAdminId) throws Exception {
        AuthSysAdminDO sysAdminDO = authSysAdminMapper.findById(sysAdminId);
        defaultAdminVerify(sysAdminDO);
        lastAdminVerify(sysAdminDO);
        return authSysAdminMapper.delete(sysAdminId);
    }

    @Override
    public int changeEnable(AuthSysAdminModel model) throws Exception {
        AuthSysAdminDO sysAdminDO = AuthSysAdminAssembler.assembleSysAdminDO(model,false);
        if(model.getEnable()==0) lastAdminVerify(sysAdminDO);
        return authSysAdminMapper.updateEnable(sysAdminDO);
    }

    @Override
    public int changeDefaultFlag(AuthSysAdminModel model) throws Exception {
        AuthSysAdminDO sysAdminDO = AuthSysAdminAssembler.assembleSysAdminDO(model,false);
        if(model.getDefaultFlag()==0) lastAdminVerify(sysAdminDO);
        return authSysAdminMapper.updateDefaultFlag(sysAdminDO);
    }

    @Override
    public int changePassword(AuthSysAdminChangeReq params) throws Exception {
        AuthSysAdminDO sysAdminDO = authSysAdminMapper.findById(params.getSysAdminId());
        if(!sysAdminDO.getPassword().equals(MD5Util.encrypt(params.getOldPassword()))){
            throw new Exception("密码不正确");
        }
        sysAdminDO.setPassword(MD5Util.encrypt(params.getPassword()));
        return authSysAdminMapper.updatePassword(sysAdminDO);
    }

    @Override
    public AuthSysAdminModel querySingleSystemAdmin(String sysAdminId) {
        AuthSysAdminDO sysAdminDO = authSysAdminMapper.findById(sysAdminId);
        AuthSysAdminModel result = AuthSysAdminAssembler.assembleSysAdminModel(sysAdminDO);
        return result;
    }

    @Override
    public AuthSysAdminModel querySingleSystemAdminByAccount(String sysAdminAccount) {
        AuthSysAdminDO sysAdminDO = authSysAdminMapper.findByAccount(sysAdminAccount);
        if(sysAdminDO == null) return null;
        AuthSysAdminModel result = AuthSysAdminAssembler.assembleSysAdminModel(sysAdminDO);
        return result;
    }

    @Override
    public List<AuthSysAdminModel> querySystemAdminList(AuthSysAdminModel params) {
        AuthSysAdminDO condition = AuthSysAdminAssembler.assembleSysAdminQueryDO(params);
        List<AuthSysAdminDO> adminDOS = authSysAdminMapper.findByCondition(condition);
        List<AuthSysAdminModel> result = ListUtils.copy(adminDOS, AuthSysAdminModel.class);
        return result;
    }

    @Override
    public ReturnList<AuthSysAdminModel> querySystemAdminList(AuthSysAdminQuery query) {
        AuthSysAdminDO condition = AuthSysAdminAssembler.assembleSysAdminQueryDO(query.getParams());
        Page<?> page = PageHelper.startPage(query.getPage().getPageNum(),query.getPage().getPageSize());
        List<AuthSysAdminDO> adminDOS = authSysAdminMapper.findByCondition(condition);
        List<AuthSysAdminModel> adminModels = ListUtils.copy(adminDOS, AuthSysAdminModel.class);
        ReturnList<AuthSysAdminModel> result = new ReturnList<>(adminModels, page.getTotal());
        return result;
    }

    private boolean defaultAdminVerify(AuthSysAdminDO sysAdminDO) throws Exception {
        AuthSysAdminDO adminDO = authSysAdminMapper.findById(sysAdminDO.getSysAdminId());
        if(adminDO.getDefaultFlag()==1){
            throw new Exception("不可移除默认系统管理员");
        }
        return true;
    }

    private boolean lastAdminVerify(AuthSysAdminDO sysAdminDO) throws Exception {
        AuthSysAdminDO condition = new AuthSysAdminDO();
        condition.setEnable(1);
        condition.setDefaultFlag(1);
        List<AuthSysAdminDO> adminDOS = authSysAdminMapper.findByCondition(condition);
        if(adminDOS.size()==1&&adminDOS.get(0).getSysAdminId().equals(sysAdminDO.getSysAdminId())){
            throw new Exception("至少存在一个可用的默认系统管理员");
        }
        return true;
    }

    private boolean sameAccountVerify(AuthSysAdminDO sysAdminDO) throws Exception {
        AuthSysAdminDO sameDO = authSysAdminMapper.findSameByAccount(sysAdminDO);
        if(sameDO!=null){
            throw new Exception("账号重复");
        }
        return true;
    }

}
